<template>
  <div>
    <el-card>
      <template #header>
        <div style="display: flex; justify-content: space-between; align-items: center;">
          <span>待审核店铺</span>
          <el-button type="primary" @click="fetchPendingStores">刷新</el-button>
        </div>
      </template>

      <el-table :data="storeList" border style="width: 100%" v-loading="loading">
        <el-table-column prop="merchantName" label="店铺名称" width="150" />
        <el-table-column prop="contactPhone" label="联系电话" width="150" />
        <el-table-column prop="address" label="店铺地址" />
        <el-table-column prop="status" label="状态" width="120">
          <template #default="{row}">
            <el-tag :type="getStatusTagType(row.status)">
              {{ formatStatus(row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="申请时间" width="180" />
        <el-table-column label="操作" width="180">
          <template #default="{row}">
            <el-button size="small" type="primary" @click="openReviewDialog(row)">审核</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 审核弹窗 -->
    <el-dialog
        v-model="reviewDialogVisible"
        title="店铺审核"
        width="50%"
        :before-close="handleClose"
    >
      <el-form :model="currentStore" label-width="100px">
        <el-form-item label="店铺名称">
          <el-input v-model="currentStore.merchantName" disabled />
        </el-form-item>

        <el-form-item label="联系电话">
          <el-input v-model="currentStore.contactPhone" disabled />
        </el-form-item>

        <el-form-item label="店铺地址">
          <el-input v-model="currentStore.address" type="textarea" disabled />
        </el-form-item>

        <el-form-item label="审核结果" prop="status">
          <el-radio-group v-model="currentStore.status">
            <el-radio :label="1">通过</el-radio>
            <el-radio :label="2">拒绝</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="审核意见" prop="remark">
          <el-input v-model="currentStore.remark" type="textarea" placeholder="请输入审核意见" />
        </el-form-item>
      </el-form>

      <template #footer>
        <span class="dialog-footer">
          <el-button @click="reviewDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="submitReview">确认</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import axios from 'axios'
import { ElMessage, ElMessageBox } from 'element-plus'

const BASE_URL = 'http://localhost:8080'
const storeList = ref([])
const loading = ref(false)
const reviewDialogVisible = ref(false)
const currentStore = ref({
  id: null,
  merchantName: '',
  contactPhone: '',
  address: '',
  status: 1,
  remark: ''
})

const fetchPendingStores = async () => {
  try {
    loading.value = true
    const response = await axios.get(BASE_URL + '/api/v1/admin/selCheckStore')
    storeList.value = response.data.data || []
  } catch (error) {
    console.error('获取待审核店铺失败:', error)
    ElMessage.error('获取待审核店铺失败')
  } finally {
    loading.value = false
  }
}

const formatStatus = (status) => {
  const statusMap = {
    0: '待审核',
    1: '已通过',
    2: '已拒绝'
  }
  return statusMap[status] || '未知'
}

const getStatusTagType = (status) => {
  const typeMap = {
    0: 'warning',
    1: 'success',
    2: 'danger'
  }
  return typeMap[status] || ''
}

const openReviewDialog = (store) => {
  currentStore.value = {
    id: store.id,
    merchantName: store.merchantName,
    contactPhone: store.contactPhone,
    address: store.address,
    status: 1,
    remark: ''
  }
  reviewDialogVisible.value = true
}

const handleClose = (done) => {
  ElMessageBox.confirm('确认要关闭审核弹窗吗?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(() => {
    done()
  }).catch(() => {})
}

const submitReview = async () => {
  try {
    loading.value = true // 添加加载状态
    const response = await axios.post(BASE_URL + '/api/v1/admin/checkStore', {
      id: currentStore.value.id,
      status: currentStore.value.status,
      remark: currentStore.value.remark
    })

    if (response.data.code == 2000) { // 使用严格相等
      ElMessage.success('审核成功')
      await fetchPendingStores() // 等待刷新完成
    } else {
      ElMessage.error(response.data.message || '审核失败')
    }
  } catch (error) {
    console.error('审核提交失败:', error)
    ElMessage.error(error.response?.data?.message || '审核提交失败')
  } finally {
    loading.value = false
    reviewDialogVisible.value = false // 确保一定会关闭
  }
}

onMounted(() => {
  fetchPendingStores()
})
</script>

<style scoped>
.el-table {
  margin-top: 20px;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
}
</style>